let {userTable} = require("./userTable")
let jwt=require("jsonwebtoken")
let bcrypt=require("bcryptjs")
module.exports={
    login(req,res){
        let {account,pass}=req.body
        let SECRET="feifei"
        userTable.find({
            userName:account
        }).then(data=>{
            // console.log(data)
            if(data.length>0){
                let valid=bcrypt.compareSync(pass,data[0].userPwd)
                const token=jwt.sign({name:data[0].userName},SECRET,{expiresIn:1000})  
                // console.log(valid,token)
                if(valid){
                    return res.send({
                        msg:"登陆成功",
                        code:200,
                        token,
                        name:data[0].userName,
                        date:new Date()
                    })
                }else{
                    return res.send({
                        msg:"密码错误,请重新输入",
                        code:400
                    })
                }
            }else{
                res.send({
                    msg:"账号不存在,请先注册",
                    code:404,
                })
            }
        })
        
    }
}